<template>
  <div>
    <el-container>
      <el-header>
        <img class="mlogo" src="../assets/logo_mrru.png" alt="">
      </el-header>

      <el-main>
        <el-form :model="user" :rules="rules" ref="user" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('user')">确定</el-button>
            <el-button @click="resetForm('user')">取消</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      user: {
        username: 'ru12321',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度应在 6 到 15 个字符', trigger: 'blur' }
        ],
      }
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var _this = this;
          this.$axios.post("/login",this.user)
            .then(res=>{
              //1.拿到jwt token值
              const jwt = res.headers.authorization;

              //2.拿到后端login接口返回的用户信息
              const userInfo = res.data.data;

              //3.保存信息到store全局变量中
              _this.$store.commit('SET_TOKEN',jwt);
              _this.$store.commit('SET_USERINFO',userInfo);

              //4.测试
              //console.log(_this.$store.state.token);
              //console.log(_this.$store.state.userInfo);

              //5.跳转到博客列表页面
              _this.$router.push({name:'Blogs'});
            })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    /*background-color: #E9EEF3;*/
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
  .mlogo{
    height: 105%;
    margin-top: -3px;
  }

  .demo-ruleForm{
    max-width: 500px;
    margin: 0 auto;
  }
</style>
